﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using System.Xml.Linq;
using Windows.Data.Xml.Dom;

namespace iSciences.Data
{
    public class RSSStream
    {
        string url;

        public string Url
        {
            get { return url; }
            set { url = value; }
        }

        Category category;

        public Category Category
        {
            get { return category; }
            set { category = value; }
        }

        public RSSStream(string url)
        {
            this.url = url;
        }

        public async Task ParseAsync()
        {
            XmlDocument document = await XmlDocument.LoadFromUriAsync(new Uri(this.url));
            IXmlNode categoryNameNode = document.SelectSingleNode("/rss/channel/title");
            IXmlNode categoryDescriptionNode = document.SelectSingleNode("/rss/channel/description");
            IXmlNode categoryImagePathNode = document.SelectSingleNode("/rss/channel/image/url");
            XmlNodeList articlesNodes = document.SelectNodes("//item");

            Category category;
            Guid categoryUniqueId;
            string categoryName;
            string categoryDescription;
            string categoryImagePath;
            List<Article> categoryArticles = new List<Article>();

            Article article;
            Guid articleUniqueId;
            string articleTitle;
            string articleSubtitle;
            string articlePubdate;
            string articleAuthor;
            string articleImageUrl;
            string articleUrl;
            string articleContent;

            //intialize a new category
            categoryUniqueId = Guid.NewGuid();
            categoryName = categoryNameNode != null ? categoryNameNode.InnerText : null;
            categoryDescription = categoryDescriptionNode != null ? categoryDescriptionNode.InnerText : null;
            categoryImagePath = categoryImagePathNode != null ? categoryImagePathNode.InnerText : null;
            category = new Category(categoryUniqueId, categoryName, categoryDescription, categoryImagePath, categoryArticles);

            //set categories articles articles;
            foreach (XmlElement item in articlesNodes)
            {
                articleUniqueId = Guid.NewGuid();
                IXmlNode articleTitleNode = item.SelectSingleNode("title");
                IXmlNode articleSubtitleNode = item.SelectSingleNode("description");
                IXmlNode articlePubdateNode = item.SelectSingleNode("pubDate");
                IXmlNode articleAuthorNode = item.SelectSingleNode("author");
                IXmlNode articleImageUrlNode = item.SelectSingleNode("enclosure");
                IXmlNode articleUrlNode = item.SelectSingleNode("link");
                //TODO content here 

                articleTitle = articleTitleNode != null ? articleTitleNode.InnerText : null;
                articleSubtitle = articleSubtitleNode != null ? articleSubtitleNode.InnerText : null;
                articlePubdate = articlePubdateNode != null ? articlePubdateNode.InnerText : null;
                articleAuthor = articleAuthorNode != null ? articleAuthorNode.InnerText : null;
                articleImageUrl = articleImageUrlNode != null ? articleImageUrlNode.Attributes[0].InnerText : null;
                articleUrl = articleUrlNode != null ? articleUrlNode.InnerText : null;
                articleContent = string.Empty;

                //add article to list
                article = new Article(articleUniqueId, articleTitle, articleSubtitle, articlePubdate,
                    articleAuthor, articleImageUrl, articleUrl, articleContent, category);
                categoryArticles.Add(article);
            }

            //category.Articles = articles;
            this.Category = category;
        }
    }
}
